package com.binance.mbxgateway.web.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.binance.mbxgateway.web.service.IMatchboxRestService;

@Service
public class MatchboxRestServiceImpl implements IMatchboxRestService {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${matchbox.rest.root.url}")
    private String restUrl;

    @Override
    public JSONArray fetchLatestKline(String symbol) {
        String url = UriComponentsBuilder.fromHttpUrl(String.format("%s/klines", restUrl)).queryParam("symbol", symbol)
                .queryParam("limit", "1").queryParam("interval", "1d").build().toUriString();
        String jsonStr = this.restTemplate.getForObject(url, String.class);
        JSONArray array = JSON.parseArray(jsonStr);
        if (array.size() > 0) {
            return array.getJSONArray(0);
        }
        return array;
    }

    @Override
    public JSONArray fetchKlineOn(String symbol, Date date) {
        long startTime = date.getTime();
        long endTime = startTime + 24 * 60 * 60 * 1000 - 1;
        String url = UriComponentsBuilder.fromHttpUrl(String.format("%s/klines", restUrl)).queryParam("symbol", symbol)
                .queryParam("limit", "2").queryParam("interval", "1d").queryParam("startTime", startTime)
                .queryParam("endTime", endTime).build().toUriString();
        String jsonStr = this.restTemplate.getForObject(url, String.class);
        JSONArray array = JSON.parseArray(jsonStr);
        if (array.size() > 0) {
            return array.getJSONArray(0);
        }
        return array;
    }

    @Override
    public String fetchKlines(String symbol, String interval, Long startTime, Long endTime, Long limit) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(String.format("%s/klines", restUrl))
                .queryParam("symbol", symbol).queryParam("interval", interval);
        if (startTime != null) {
            builder.queryParam("startTime", startTime);
        }
        if (endTime != null) {
            builder.queryParam("endTime", endTime);
        }
        if (limit != null) {
            builder.queryParam("limit", limit);
        }
        String url = builder.toUriString();
        return this.restTemplate.getForObject(url, String.class);
    }

    @Override
    public String get24hrTicker(String symbol) {
        UriComponentsBuilder builder =
                UriComponentsBuilder.fromHttpUrl(String.format("%s/ticker/24hr", restUrl)).queryParam("symbol", symbol);
        return restTemplate.getForObject(builder.build().toUriString(), String.class);
    }

}
